package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.epfresh.api.constant.AppSupplyRouterConstant;
import com.epfresh.api.entity.RequestEntityMapSupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.widget.MultipleStatusView;
import com.epfresh.api.widget.ptr.PtrDefaultHandler;
import com.epfresh.api.widget.ptr.PtrFrameLayout;
import com.epfresh.api.widget.ptr.PtrHandler;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.JoinPrepareGridViewBinder;
import com.epfresh.supplier.adapter.JoinSalePrepareGridViewBinder;
import com.epfresh.supplier.entity.JoinPrepareGrid;
import com.epfresh.supplier.entity.JoinProductItems;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

/*联营备货*/
public class JoinPrepareGridActivity extends BaseActivity {

    //列表数据
    List<Object> mItems = new ArrayList<>();
    MultiTypeAdapter mMultiAdapter;
    JoinPrepareGridViewBinder itemViewBinder;
    JoinSalePrepareGridViewBinder saleitemViewBinder;
    GridLayoutManager layoutManagerGoods;
    RecyclerView rv;
    int pageNum = 0;
    private MultipleStatusView msvGoods;


    private PtrFrameLayout ptrFrameLayout;
    private PtrHandler ptrHandler = new PtrHandler() {
        @Override
        public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
            return PtrDefaultHandler.checkContentCanBePulledDown(frame, rv, header);
        }

        @Override
        public void onRefreshBegin(PtrFrameLayout frame) {
            freshFlag = true;
            reqSupplyStoreDyn(0);
        }
    };

    boolean freshFlag;

    String name, productId, merchantId, receiveTimeStr;
    private String goodId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_goods_grid);
        goodId = getIntent().getStringExtra("goodId");
        if (goodId != null) {
            //销售订单  联营订单 goodId
            toolbarTitle.setText(Html.fromHtml("<font color=\"#00ff7900\">[联营]</font>"));
        } else {
            name = getIntent().getStringExtra("name");
            productId = getIntent().getStringExtra("productId");
            merchantId = getIntent().getStringExtra("merchantId");
            receiveTimeStr = getIntent().getStringExtra("receiveTimeStr");
            boolean isJoint = getIntent().getBooleanExtra("isJoint", true);
            if (isJoint) {
                toolbarTitle.setText(Html.fromHtml("<font color=\"#00ff7900\">[联营]</font>" + name));
            } else {
                toolbarTitle.setText(Html.fromHtml("<font color=\"#0001bd94\">[自营]</font>" + name));
            }
            //vh.tv_title.setText(Html.fromHtml(html));


        }
        initView();
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_make_agent_back);
        initHeadView();
        rv = (RecyclerView) findViewById(R.id.rv_goods);
        msvGoods = (MultipleStatusView) findViewById(R.id.msv_goods);
        msvGoods.setHint("已完成商品分拣，商品正在调度中\n请回到前一页，下拉刷新");
        msvGoods.setIcon(R.mipmap.join_grid_no);


        layoutManagerGoods = new GridLayoutManager(this, 2);

        rv.setLayoutManager(layoutManagerGoods);

        mMultiAdapter = new MultiTypeAdapter();
        if (goodId != null) {
            saleitemViewBinder = new JoinSalePrepareGridViewBinder();
            mMultiAdapter.register(JoinProductItems.ProductBean.class, saleitemViewBinder);
        } else {
            itemViewBinder = new JoinPrepareGridViewBinder();
            mMultiAdapter.register(JoinPrepareGrid.SupplyProductDtoBean.ItemsBean.class, itemViewBinder);
        }

        rv.setAdapter(mMultiAdapter);
        mMultiAdapter.setItems(mItems);
        reqSupplyStoreDyn(pageNum);

    }


    private void initHeadView() {
        ptrFrameLayout = (PtrFrameLayout) findViewById(R.id.ptr_ff);
        ptrFrameLayout.setPtrHandler(ptrHandler);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

    }

    public void reqSupplyStoreDyn(int pageNum) {
        RequestEntityMapSupply RequestEntityMapSupply = new RequestEntityMapSupply();
        if (null != goodId) {
            RequestEntityMapSupply.setCmd(AppSupplyRouterConstant.cmd_order_supplier_order_stock_joinProductItems);
//        RequestEntityMapSupply.putParameter("dateStr", receiveTimeStr);
            RequestEntityMapSupply.putParameter("id", goodId);
            request(RequestEntityMapSupply, AppSupplyRouterConstant.cmd_order_supplier_order_stock_joinProductItems, joinProductItemsRequestListener);
        } else {
            RequestEntityMapSupply.setCmd(AppSupplyRouterConstant.cmd_order_supplier_order_stock_list_product);
//        RequestEntityMapSupply.putParameter("dateStr", receiveTimeStr);
            RequestEntityMapSupply.putParameter("receiveTimeStr", receiveTimeStr);
            RequestEntityMapSupply.putParameter("specId", productId);
            RequestEntityMapSupply.putParameter("merchantId", merchantId);
            request(RequestEntityMapSupply, AppSupplyRouterConstant.cmd_order_supplier_order_stock_list_product, onSupplyStoreDynRequestListener);
        }
    }

    private OnRequestListener<JoinProductItems> joinProductItemsRequestListener = new OnRequestListener<JoinProductItems>() {
        @Override
        public JoinProductItems jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, JoinProductItems.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            ptrFrameLayout.refreshComplete();
            msvGoods.showError();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            ptrFrameLayout.refreshComplete();
            hideProgressDialog();
            msvGoods.showError();
        }

        @Override
        public void onStart(Object tag) {
            if (!freshFlag) {
                showProgressDialog();
                freshFlag = false;
            }
        }

        @Override
        public void onResponse(ResponseEntity<JoinProductItems> response, Object tag) {
            hideProgressDialog();
            ptrFrameLayout.refreshComplete();
            mItems.clear();
            JoinProductItems responseElement = response.getResponseElement();
            if (null != responseElement && !TextUtils.isEmpty(responseElement.getTitle())) {
                toolbarTitle.setText(Html.fromHtml("<font color=\"#00ff7900\">[联营]</font>" + responseElement.getTitle()));
            }
            if (saleitemViewBinder != null)
//                saleitemViewBinder.setChargeUnit(responseElement.);
                mItems.addAll(responseElement.getProduct());
            if (mItems.size() == 0) {
                //loadMoreDelegate.setStatusEmpty();
                msvGoods.showEmpty();
            } else {
                msvGoods.showContent();
            }
            mMultiAdapter.notifyDataSetChanged();
        }
    };

    private OnRequestListener<JoinPrepareGrid> onSupplyStoreDynRequestListener = new OnRequestListener<JoinPrepareGrid>() {
        @Override
        public JoinPrepareGrid jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, JoinPrepareGrid.class);

        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            ptrFrameLayout.refreshComplete();
            msvGoods.showError();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            ptrFrameLayout.refreshComplete();
            hideProgressDialog();
            msvGoods.showError();
        }

        @Override
        public void onStart(Object tag) {
            if (!freshFlag) {
                showProgressDialog();
                freshFlag = false;
            }
        }

        @Override
        public void onResponse(ResponseEntity<JoinPrepareGrid> response, Object tag) {
            hideProgressDialog();
            ptrFrameLayout.refreshComplete();
            mItems.clear();
            JoinPrepareGrid responseElement = response.getResponseElement();
            if (itemViewBinder != null)
                itemViewBinder.setChargeUnit(responseElement.getSupplyProductDto().getChargeUnit());
            mItems.addAll(responseElement.getSupplyProductDto().getItems());
            if (mItems.size() == 0) {
                //loadMoreDelegate.setStatusEmpty();
                msvGoods.showEmpty();
            } else {
                msvGoods.showContent();
            }
            mMultiAdapter.notifyDataSetChanged();
        }
    };

    @Override
    public void onSaveInstanceState(Bundle outState) {
        //记录当前fragment的下标
        /*outState.putInt(Constant.KEY_HOME_STATE_INDEX, currentFragmentIndex);
        outState.putInt("orderType", orderType);*/
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x65 && resultCode == RESULT_OK) {
        }
    }
}
